<?php

/**
 * Users Model
 *
 * Interacts with the users table in the database.
 *
 * @author Kris Suchdeve
 */

class Users_model extends Model {

                /**
                 * Name of Function: Constructor
                 *
                 * Detailed Description: This is my constructor
                 * that automatically loads when this class is instantiated
                 *
                 * @author Kris Suchdeve
                 */

    function Users_model()
    {
        parent::Model();
                                $this->load->database();
                                $this->load->library('encrypt');
    }
                function retrieve_user($user_id)
                {
                        //"SELECT * FROM Admin WHERE `Username` = '$user_id'";
                        $result = $this->db->get_where('Admin', array('Username' => $user_id));

                        if ($result->num_rows() == 1)
                                return $result->row();
                        else
                                return FALSE;
                }

                /**
                 * Add User
                 *
                 * Adds a user to the database.
                 *
                 * @param string $email
                 * @param string $pass
                 * @return boolean
                 */

                function add_user($admin_login, $admin_pass)
                {
                        //Load encryption
                        $this->load->library('encrypt');

                        //Get the values
                        $data = array();
                        $data['admin_login'] = $admin_login;
                        $data['admin_pass'] = $this->encrypt->sha1($admin_pass);

                        //Try it...
                        $result = $this->db->insert('Admin', $data);
                        return $result;
                }

                function check_credentials($un, $pw)
                {
                        $this->load->library('encrypt');

                        //"SELECT * FROM Admin WHERE `Username` = '$un';";
                        $query = $this->db->get_where('Admin', "`Username` = '$un'");

                        if ($query->num_rows() == 1)
                        {
                                //good we got a user!
                                $pw = $this->encrypt->sha1($pw);
                                $result = $query->row();
                                if ($result->password == $pw)
                                        return $result->id;
                                else
                                        return FALSE;
                        }
                        else
                                return FALSE;
                }
}

/* EOF */
